/*
 * StopwatchPanel.java
 *
 * Created on 11 de Agosto de 2010, 18:48
 */
package br.ufc.stopwatchplugin.panel;

import java.util.Calendar;
import java.util.Timer;

/**
 *
 * @author  nelson
 */
public class StopwatchPanel extends javax.swing.JPanel {

    private static boolean count = false;
    private static boolean reset = false;
    private Timer timer;
    private Chronometer chronometer;
    private Calendar calendar;

    /** Creates new form StopwatchPanel */
    public StopwatchPanel() {
        initComponents();
    }

    public static boolean isCount() {
        return count;
    }

    public static boolean isReset() {
        return reset;
    }

    public static void setReset(boolean reset) {
        StopwatchPanel.reset = reset;
    }

    private void start() {
        count = true;

        chronometer = new Chronometer(btnTime, calendar);
        this.timer = new Timer();
        this.timer.scheduleAtFixedRate(this.chronometer, 0, 1000);
    }

    private void pause() {
        count = false;
        this.calendar = chronometer.getCalendar();
    }

    private void reset() {
        btnTime.setText("00:00:00");
        count = false;
        reset = true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnTime = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        btnTime.setFont(new java.awt.Font("DejaVu Sans", 0, 16)); // NOI18N
        btnTime.setForeground(java.awt.SystemColor.desktop);
        btnTime.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/ufc/stopwatchplugin/panel/chronometer.png"))); // NOI18N
        btnTime.setText(org.openide.util.NbBundle.getMessage(StopwatchPanel.class, "StopwatchPanel.btnTime.text")); // NOI18N
        btnTime.setBorderPainted(false);
        btnTime.setFocusPainted(false);
        btnTime.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnTimeMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnTime)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnTime)
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnTimeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnTimeMouseClicked
    if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {

        //for all actions, cancel the last timer and initialize other.
        if (timer != null) {
            this.timer.cancel();
        }

        if (evt.getClickCount() >= 2) {
            reset();
        } else if (evt.getClickCount() == 1) {
            if (!isCount()) {
                start();
            } else {
                pause();
            }
        }
    }
}//GEN-LAST:event_btnTimeMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btnTime;
    // End of variables declaration//GEN-END:variables
}
